package com.yyi.springcloud.yyi.controller;



import com.yyi.springcloud.yyi.bean.AlarmMessage;
import com.yyi.springcloud.yyi.bean.warpper.AlaramListMessage;
import com.yyi.springcloud.yyi.bean.warpper.Content;
import com.yyi.springcloud.yyi.bean.warpper.TestData;
import com.yyi.springcloud.yyi.service.AlarmMessageService;
import com.yyi.springcloud.yyi.service.ContentService;
import lombok.Data;
import org.apache.solr.common.util.Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.stream.Location;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class yyController {
    @Data
    private static class Location{
         String field;
         String title;
         String templet;
         String align;
    }

    @Autowired
    private AlarmMessageService alarmMessageService;
    @Autowired
    private ContentService contentService;
    @RequestMapping("/getAlarmMessageList")
    @CrossOrigin
    public Map getAlarmMessageList(){
        List<AlaramListMessage> list = alarmMessageService.getAlarmList();
        HashMap map = new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("count",list.size());
        for (AlaramListMessage alarmMessage:list){
            if (alarmMessage.getIfproductFault().equals("0")){
                alarmMessage.setIfproductFault("是");
            }else{
                alarmMessage.setIfproductFault("否");
            }

        }
        map.put("data",list);
        return map;
    }
    @CrossOrigin
    @RequestMapping("/getContentList")
    public List<Content> getContentList(){
        List list =  contentService.getContentList();
        Location location = new Location();
        location.setField("location");
        location.setTitle("定位");
        location.setTemplet("#buttonTpl");
        location.setAlign("center");
        list.add(location);
        return list;

    }
    //layui数据列表
    @RequestMapping("/getDataList/{parimarykey}")
    @CrossOrigin
    public Map getDataList(@PathVariable("parimarykey") String parimarykey ){
        HashMap map = new HashMap();
        List li = new ArrayList();
        List<TestData> list = alarmMessageService.getTestDataList(parimarykey);
        for (int i=0;i<list.size();i++){
            String tempValue = list.get(i).getSensorValue();
            String arr[] = tempValue.split("@");
            HashMap te = new HashMap();
            for (int j = 1;j<arr.length;j++){
                String key = arr[j].split(":")[0];
                String value = arr[j].split(":")[1];
                te.put(key,value);
            }
            String date = list.get(i).getAcqusitionTime();
            te.put("acqusitionDate",date.split(" ")[0]);
            te.put("acqusitionTime",date.split(" ")[1]);
            li.add(te);
        }
        map.put("data",li);
        map.put("count",list.size());
        map.put("code",0);
        map.put("msg","");

        return map;
    }
}
